﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace commonlib
{
    public class PuppetMasterServices : IPuppetMasterServices
    {
        #region IPuppetMasterServices Members

        CentralDirectoryServices directoryServices;

        public List<DataServer> dataServers
        {
            get;
            set;
        }

        public PuppetMasterServices()
        {
            dataServers = new List<DataServer>();
        }

        public bool init()
        {
            try
            {
                
                directoryServices = (CentralDirectoryServices)Activator.GetObject(
                                                             typeof(CentralDirectoryServices),
                                                             "tcp://" + this.centralAddress + ":" + this.centralPort + "/CentralDirectoryServices");
                this.dataServers = directoryServices.getRegisteredDataServers();
                return true;
            }
            catch (SocketException)
            {
                System.Console.WriteLine("Nao foi possivel contactar o Central Directory.");
            }

            return false;
        }

        public bool updateDataServers()
        {
            if (this.directoryServices != null)
            {
                this.dataServers = this.directoryServices.getRegisteredDataServers();
                return true;
            }/*
            else
            {
                init();
                this.dataServers = this.directoryServices.getRegisteredDataServers();
                return true;
            }*/

            else
                return false;
        }

        public string centralAddress
        {
            get;
            set;
        }

        public int centralPort
        {
            get;
            set;
        }

        /**
         * Modificar o estado de um data server
         **/
        public bool changeDataServerState(string name, STATES state)
        {

            DataServer dataServer = null;
            try
            {
                if (this.directoryServices != null)
                {
                    this.dataServers = directoryServices.getRegisteredDataServers();
                    dataServer =  getDataServer(name);
                    return true;
                }
                else
                    return false;
                
            }
            catch (SocketException)
            {
               System.Console.WriteLine("Não foi possivel contactar o dataserver {0}", name);
            }

            if(dataServer != null)
            {
                DataServerServices dataServerServices = (DataServerServices)Activator.GetObject(
                                                        typeof(CentralDirectoryServices),
                "tcp://" + dataServer.ipAddress + ":" + dataServer.port + "/DataServerServices");

                try
                {
                    dataServerServices.setState(name,state);
                    return true;
                    //Console.ReadKey();
                }
                catch (SocketException)
                {
                    System.Console.WriteLine("Nao foi possivel contactar o Central Directory.");
                }
            }

            return false;

        }

        public DataServer getDataServer(string name)
        {
            Console.WriteLine("search for " + name + " size:" + dataServers.Count);
            DataServer ds = null;

            /*
            foreach(DataServer dataServer in this.registeredDataServers){
                if(dataServer.name.Equals(name))
                    return dataServer;
            }*/

            return ds;
        }

        public bool executeClientCommand(string command)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
